package edu.bit.water.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.bit.water.entity.CleanProduct;
import edu.bit.water.service.CleanProductService;

@Controller
@RequestMapping("/clean")
public class CleanProductController {
	
	@Autowired
	CleanProductService cleanProductService;
	
	@ResponseBody
    @RequestMapping("/insert")
	public Map<String, Object> insert(CleanProduct cleanProduct){
		Map<String, Object> modelMap = new HashMap<>();		
		float rank1=0;
		float rank2=0;
		float rank3=0;
		try {
			rank1 = (float) (cleanProduct.getProductWaterSum()/cleanProduct.getProductSum());
			rank2 = (float) (cleanProduct.getProductWasteSum()/cleanProduct.getProductSum());
			rank3 = (float) (cleanProduct.getCOG()/cleanProduct.getProductSum());
		}catch (Exception e) {
			e.printStackTrace();
		}
		String r1,r2,r3;
		if(rank1 <= 2.0) {
			r1 = "1";
		}else if(rank1 <= 3.0) {
			r1 = "2";
		}else if(rank1 <= 3.8) {
			r1= "3";
		}else {
			r1 = "不合格";
		}
		if(rank2 <= 1.6) {
			r2 = "1";
		}else if(rank2 <= 2.4) {
			r2 = "2";
		}else if(rank2 <= 3.0) {
			r2= "3";
		}else {
			r2 = "不合格";
		}
		if(rank3 <= 1.4) {
			r3 = "1";
		}else if(rank3 <= 2.0) {
			r3 = "2";
		}else if(rank3 <= 2.5) {
			r3= "3";
		}else {
			r3 = "不合格";
		}
		String rank = r1+"/"+r2+"/"+r3;
		cleanProduct.setRank(rank);
		int n = cleanProductService.insert(cleanProduct);
		if(n == 1) {
	    	modelMap.put("status", "success");
	    }
	    else
	    	modelMap.put("status", "failed");
		return modelMap;
		
	}
	
	@ResponseBody
    @RequestMapping("/delete")
    public Map<String, String> delete(@RequestBody List<Integer> idList) {
        int n = cleanProductService.deleteChart(idList);
        Map<String, String> map = new HashMap<>();
        if (n == 1)
            map.put("status", "success");
        else
            map.put("status", "failed");
        return map;
    }
	
	 @ResponseBody
	    @RequestMapping("/update")
	    public Map<String, String> update(CleanProduct cleanProduct) {
		 float rank1=0;
			float rank2=0;
			float rank3=0;
			try {
				rank1 = (float) (cleanProduct.getProductWaterSum()/cleanProduct.getProductSum());
				rank2 = (float) (cleanProduct.getProductWasteSum()/cleanProduct.getProductSum());
				rank3 = (float) (cleanProduct.getCOG()/cleanProduct.getProductSum());
			}catch (Exception e) {
				e.printStackTrace();
//				System.out.println(e);
			}
			String r1,r2,r3;
			if(rank1 <= 2.0) {
				r1 = "1";
			}else if(rank1 <= 3.0) {
				r1 = "2";
			}else if(rank1 <= 3.8) {
				r1= "3";
			}else {
				r1 = "不合格";
			}
			if(rank2 <= 1.6) {
				r2 = "1";
			}else if(rank2 <= 2.4) {
				r2 = "2";
			}else if(rank2 <= 3.0) {
				r2= "3";
			}else {
				r2 = "不合格";
			}
			if(rank3 <= 1.4) {
				r3 = "1";
			}else if(rank3 <= 2.0) {
				r3 = "2";
			}else if(rank3 <= 2.5) {
				r3= "3";
			}else {
				r3 = "不合格";
			}
			String rank = r1+"/"+r2+"/"+r3;
			cleanProduct.setRank(rank);
	        return cleanProductService.updateChart(cleanProduct);
	    }
	 
	 @RequestMapping("/select")
	 @ResponseBody
	 public Map<String, Object> select(int label_id){
		 Map<String, Object> modelMap = new HashMap<>();
		 List<CleanProduct> list = cleanProductService.select(label_id);
		 modelMap.put("status", "success");
	     modelMap.put("rows", list);
    	 return modelMap;
		}

}
